home *** CD-ROM | disk | FTP | other *** search
- Path: news.ichange.com!newsmaster
- From: patrick_widener@mail.amsinc.com (Patrick Widener)
- Newsgroups: comp.lang.c++
- Subject: Re: Why is a temporary created here?
- Date: Wed, 07 Feb 1996 14:12:38 GMT
- Organization: American Management Systems, Inc.
- Message-ID: <4fac91$eur@ias2.ichange.com>
- References: <47ts5e$21o@ams.amsinc.com> <47vvhh$9ce@gabi.gabi-soft.fr>
- Reply-To: patrick_widener@mail.amsinc.com
- NNTP-Posting-Host: 162.70.176.43
- X-Newsreader: Forte Agent .99d/32.182
-
- kanze@gabi-soft.fr (J. Kanze) wrote:
-
- >Patrick Widener (patrick_widener@mail.amsinc.com) wrote:
- >
- >|> I have this situation:
- >
- >|> class B
- >|> {
- >|> ...
- >|> };
- >
- >|> class DOne : public B
- >|> {
- >|> ...
- >|> };
- >
- >|> class DTwo : public B
- >|> {
- >|> public:
- >|> void alfa ();
- >
- >|> protected:
- >
- >|> void bravo( B* &);
- >|> };
- >
- >|> void DTwo::alfa
- >|> {
- >|> DOne *pDOne;
- >|> DTwo *pDTwo;
- >|>
- >|> bravo (pDOne); // These lines are flagged by the compiler as
- >|> bravo (pDTwo); // warnings because temporaries are used for
- >|> // the formal argument of DTwo::bravo()
- >|> }
- <snip>
- >According to the current draft, binding a non-lvalue to a non-const
- >reference is illegal, since any changes made through the reference will
- >only modify the temporary. This change was introduced at a later stage
- >in the language evolution (about 1990, I think), and most compilers only
- >generate a warning, rather than an error, to avoid breaking existing
- >code. But it is still illegal.
- >
- >From the above, it is not clear what you really want to do. Perhaps
- >declaring the parameter to bravo const would be enough, although in this
- >case, since you are dealing with pointers, just pass be value would have
- >the same effect. Alternatively, you could declare pDOne and pDTwo as
- >B*. Or create two bravo functions, one for each type. Or use an
- >explicit temporary of the correct type, and then assign it back to pDOne
- >or pDTwo after bravo returns.
-
- OK. Probably more information in the original problem statement would
- have helped a little. DTwo actually looks a little more like this:
-
- class DTwo : public B
- {
- public:
- void alfa ();
- protected:
- bool bravo (const Dictionary&, const String&, B* &);
-
- Dictionary DOneDict;
- Dictionary DTwoDict;
- };
-
- What I actually wanted to do is to use the DTwo::bravo() method to
- pick a specified DOne or DTwo pointer out of either DTwo::DOneDict or
- DTwo::DTwoDict based on the String that I pass it. So, to retrieve a
- DOne I would call bravo (DOneDict, "key string", pDOne) and likewise
- bravo (DTwoDict, "key string 2", pDTwo) to retrieve a particular DTwo.
- (Yes, I know this probably isn't the best way to design this). So,
- the need for an lvalue is evident.
-
- I thought this was going to work based on the polymorphic behavior of
- the pointers. However, I seem to remember something about parameter
- passing being modeled on initialization rather than assignment, so I
- would guess that might cause a problem. oh well.
-
- Thanks for responding...
-
- --------
- patrick widener
- patrick_widener@mail.amsinc.com http://pwidener.amsinc.com:2112
- "Ted Striker has more guts in his big toe than most of us have in
- our entire large intestine - INCLUDING the colon!!"
-